package com.gwt.auth.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.util.StringUtils;

import java.util.Date;

@AutoConfiguration
public class AuthFilterConfig {

    /**
     * Redis根目录jKey
     */
    public static final String AUTH_REDIS_ROOT_KEY = "GWT:SECURITY:";

    /**
     * 前端传递token key
     */
    private static final String AUTH_TOKEN_KEY = "G-Token";

    /**
     * 缓存、Redis存储用户信息时间，单位：毫秒秒，默认一小时
     */
    private static final Long AUTH_TOKEN_TIME = 60 * 60 * 1000L;

    /**
     * Request-UserId
     */
    private static final String AUTH_USER_ID_KEY = "userId";

    /**
     * 用户信息缓存方式-Redis(默认)
     */
    private static final String AUTH_TYPE_REDIS = "REDIS";

    /**
     * token
     */
    @Value("${gwt.auth.token.key:}")
    private String authToken;

    /**
     * 有效时间，单位：秒
     */
    @Value("${gwt.auth.token.time:}")
    private Long authTokenTime;

    /**
     * Request用户ID
     */
    @Value("${gwt.auth.user.id.key:}")
    private String authUserIdKey;

    /**
     * 用户信息缓存方式
     */
    @Value("${gwt.auth.type:}")
    private String authType;

    public String getAuthToken() {
        return StringUtils.hasText(authToken) ? authToken : AuthFilterConfig.AUTH_TOKEN_KEY;
    }

    public long getAuthTokenTime() {
        return authTokenTime != null ? authTokenTime : AuthFilterConfig.AUTH_TOKEN_TIME;
    }

    public String getAuthUserIdKey() {
        return StringUtils.hasText(authUserIdKey) ? authUserIdKey : AuthFilterConfig.AUTH_USER_ID_KEY;

    }

    /**
     * 获取token超时时间
     * @return Date
     */
    public Date getExpDate(){
        long nowMillis = System.currentTimeMillis();
        long expMillis = nowMillis + getAuthTokenTime();
        return new Date(expMillis);
    }
}
